import { ORDER_CHANNEL } from "../enum/pd-enum";
import { UniHelp } from "../libs/uni-help";
import singleton from "./Singleton";

interface AudioSourceItem {
	[key : number] : {
		origin : string,
		tempFilePath : string,
	}
}
interface AudioSource {
	newOrderNotice : AudioSourceItem,
	receiveOrder : AudioSourceItem,
	startSchedule : AudioSourceItem,
	arrivePassengerDeparture : AudioSourceItem,
	pickupCustomer : AudioSourceItem,
	customerArrive : AudioSourceItem,
	finish : AudioSourceItem,
};

const host = import.meta.env.MODE === 'development' ? 'https://kctest.yuncun.com.cn/api/asset/voices' : 'https://kct.yuncun.com.cn/api/asset/voices';

const audioSource : AudioSource = {
	//新订单提醒
	newOrderNotice : {
		0 : {
			origin : `${host}/11.newOrderTips.wav`, //源
			tempFilePath : ''//下载后临时路径
		}
	},
	
	//确认接单
	receiveOrder : {
		0 : {
			origin : `${host}/1.receiveOrder.wav`,
			tempFilePath : ''
		},
		[ORDER_CHANNEL.ykp] : {//易快跑订单确认接单
			origin : `${host}/1.receiveOrderYKP.wav`,
			tempFilePath : ''
		},
		[ORDER_CHANNEL.didi] : {//滴滴订单确认接单
			origin : `${host}/1.receiveDDOrder.wav`,
			tempFilePath : ''
		}
	},
	
	//开始行程
	startSchedule : {
		0 : {
			origin : `${host}/2.start.wav`,
			tempFilePath : ''
		},
		[ORDER_CHANNEL.didi] : {//滴滴开始行程
			origin : `${host}/2.startDD.wav`,
			tempFilePath : ''
		}
	},
	
	//到达上车点
	arrivePassengerDeparture : {
		0 : {
			origin : `${host}/3.arriveBoardingPoint.wav`,
			tempFilePath : ''
		}
	},
	
	//接到乘客
	pickupCustomer : {
		0 : {
			origin : `${host}/4.customerPicked.wav`,
			tempFilePath : ''
		},
		[ORDER_CHANNEL.didi] : {//滴滴接到乘客
			origin : `${host}/4.customerPickedDD.wav`, 
			tempFilePath : ''
		}
	},
	
	//送达乘客
	customerArrive : {
		0 : {
			origin : `${host}/5.customerArrive.wav`, 
			tempFilePath : ''
		},
		[ORDER_CHANNEL.didi] : {//滴滴接到乘客
			origin : `${host}/5.customerArriveDD.wav`,	//滴滴送达乘客
			tempFilePath : ''
		},
		[ORDER_CHANNEL.ykp] : {//易快跑 送达乘客,
			origin : `${host}/5.customerArriveYKP.wav`, 
			tempFilePath : ''
		},
	},
	
	//行程结束
	finish : {
		0 : {
			origin : `${host}/6.finish.wav`,
			tempFilePath : ''
		}
	},
};

class VoiceMgr {
	/*
	声音资源
	键0 为默认音频
	origin : 网络地址
	tempFilePath : 下载后的微信临时地址
	*/
	private audioSurce : AudioSource = audioSource;
	private audioCtx : UniApp.InnerAudioContext;
	constructor () {
		uni.setInnerAudioOption({
			obeyMuteSwitch : false,
		});
		this.audioCtx = uni.createInnerAudioContext();
		this.audioCtx.autoplay = true;
	}
	
	/**
	 * @param {string} type 类型
	 * @param {number} channel 渠道 默认值为0，
	 */
	async play (type : keyof AudioSource, channel = 0) {
		const source = this.getSource(type, channel);
		//如果没有下载过音频，先下载,ios要下载后才能播放
		if (!source.tempFilePath) {
			source.tempFilePath = await UniHelp.downloadFile(source.origin);
		};
		this.audioCtx.stop();
		this.audioCtx.src = source.tempFilePath;
		this.audioCtx.play();
	}
	
	private getSource (type : keyof AudioSource, channel : ORDER_CHANNEL) {
		//优先使用特殊配置，如果没有，则使用通用语音
		return this.audioSurce[type][channel] || this.audioSurce[type][0];
	}
}


const SingleAudioMgr = singleton(VoiceMgr);
const voiceMgr = new SingleAudioMgr();
export default voiceMgr;